import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:online_study/base/AlertUtils.dart';
import 'package:online_study/base/app_title.dart';
import 'package:online_study/mine/exam/exam_bean.dart';
import 'package:online_study/mine/exam/exam_item.dart';
import 'package:online_study/theme/colors.dart';
import 'package:online_study/theme/size.dart';

//开始考试界面
class StartExamPage extends StatefulWidget {
  @override
  _StartExamPageState createState() => _StartExamPageState();
}

class _StartExamPageState extends State<StartExamPage> {
  List<ExamBean> _list = [];

  @override
  void initState() {
    super.initState();
    [1, 2, 3, 4, 5, 6, 7, 8, 9].forEach((element) {
      var examBean = ExamBean();
      if (element > 4) {
        examBean.isMulti = true;
      } else {
        examBean.isMulti = false;
      }
      examBean.question = "提升自身形象的三大要素";
      List<String> result = ["A . 肢体语言", "B . 着装", "C . 专业技能", "D . 过度饮酒"];
      examBean.answers = result;
      _list.add(examBean);
    });
    Timer(Duration(milliseconds: 1000 * 60), () => {_autoCommitDialog()});
  }

  void _autoCommitDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertUtils.haveCloseDialog(
              "本次答题时长已到", ThemeColor.color333333, "系统已自动提交本次训练", "好的，知道了", () {
            Get.back();
            Get.back();
          }, () {
            Get.back();
            Get.back();
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: BackButton(
            color: ThemeColor.color333333,
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertUtils.noCloseAlertDialog(
                        "您未提交考试结果",
                        "本次训练将自动作废",
                        "确定退出",
                        () {
                          Get.back();
                          Get.back();
                        },
                        "继续答题",
                        () {
                          Get.back();
                        });
                  });
            },
          ),
          titleSpacing: 0,
          elevation: ThemeSize.appElevation,
          backgroundColor: ThemeColor.colorWhite,
          title: AppTitle("考试"),
          actions: [
            Container(
              alignment: Alignment.center,
              margin: EdgeInsets.only(right: ThemeSize.width19),
              child: Text("剩余时间：29:59",
                  style: TextStyle(
                      color: ThemeColor.colorFA5151, fontSize: ThemeSize.sp13)),
            ),
          ],
        ),
        body: Column(
          children: [
            // Container(
            //   color: ThemeColor.colorWhite,
            //   alignment: Alignment.centerLeft,
            //   height: ThemeSize.height36,
            //   padding: EdgeInsets.only(left: ThemeSize.width19),
            //   child: Text("题目",style: TextStyle(color: ThemeColor.color333333,fontSize: ThemeSize.sp13,fontWeight: FontWeight.bold)),
            // ),
            Expanded(
                child: ListView.separated(
                    itemBuilder: (BuildContext context, int index) {
                      var examBean = _list[index];
                      examBean.index = index + 1;
                      return ExamItemPage(examBean);
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Divider(height: ThemeSize.width1);
                    },
                    itemCount: _list.length)),
            Divider(height: ThemeSize.width1),
            Container(
              color: ThemeColor.colorWhite,
              alignment: Alignment.center,
              height: ThemeSize.height84,
              child: MaterialButton(
                height: ThemeSize.height36,
                color: ThemeColor.color1485EE,
                child: Text("考试完毕，提交",
                    style: TextStyle(
                        color: ThemeColor.colorWhite,
                        fontSize: ThemeSize.sp16)),
                shape: RoundedRectangleBorder(
                    borderRadius:
                        BorderRadius.all(Radius.circular(ThemeSize.height18))),
                onPressed: () {
                  _ensureCommit();
                },
              ),
            )
          ],
        ));
  }

  void _ensureCommit() {
    String fold = _list.fold(
        "",
        (previousValue, element) => (element.choiceAnswers.isEmpty)
            ? previousValue + element.index.toString() + "、"
            : previousValue + "");
    if (fold.isEmpty) {
      String title = "确定提交?";
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertUtils.noCloseAlertDialog(
                title,
                "",
                "再看看",
                () {
                  Get.back();
                },
                "确定提交",
                () {
                  Get.back();
                  Get.back();
                });
          });
    } else {
      String title = "第" + fold + "未答, 确定提交?";
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertUtils.noCloseAlertDialog(
                title,
                "",
                "确定提交",
                () {
                  Get.back();
                  Get.back();
                },
                "继续答题",
                () {
                  Get.back();
                });
          });
    }
  }
}
